

/*
summary stats and plots for auction data

output:
- t1a_*.tex: tables with various auction summary stats
- f1a_n_aucs_year.pdf: figure with #/auctions per year
- f1a_b2c*.pdf: figure of time series of b2c

*/


* plotting options
set scheme lean_uncluttered


********************************************************************************
********************************************************************************
* summary stats and figures for auctions

* summary table (in sample)
use ../data/auction_notesbonds_govpx_shock.dta, clear

eststo clear

* dollar variables in billions
gen double offeringAmount_bil = offeringAmount / 1e9
gen double totalTendered_bil = totalTendered / 1e9
* easy to read labels
label var offeringAmount_bil "Offering Amount (billions)"
label var totalTendered_bil "Total Tendered (billions)"
label var b2c_all "Bid-to-Cover"
label var term_num "Term (Years)"
label var highYield "High Yield"

* include spacing for latex
label var b2c_directBidder "\qquad Direct Bidders"
label var b2c_indirectBidder "\qquad Indirect Bidders"
label var b2c_primaryDealer "\qquad Primary Dealers"

label var frac_depository "\qquad Depository Institutions"
label var frac_individuals "\qquad Individuals"
label var frac_dealers "\qquad Dealers"
label var frac_pensions "\qquad Pensions"
label var frac_investmentFunds "\qquad Investment Funds"
label var frac_foreign "\qquad Foreign"
label var frac_other "\qquad Other"

label var auctionDate "Auction Date"

* basic stats, full sample and subset
local auc_stats offeringAmount_bil totalTendered_bil term_num ///
	highYield b2c_all
* additional bid-to-cover stats (drop non-comp)
local auc_stats_b2c b2c_directBidder b2c_indirectBidder b2c_primaryDealer
* additional fraction won statistics
local auc_stats_frac frac_depository frac_individuals frac_dealers ///
	frac_pensions frac_investmentFunds frac_foreign frac_other

* convert fractions to percent
foreach v of varlist `auc_stats_frac' {
	replace `v' = 100*`v'
}

* for generating labels in output tables
local table_cell_str `" mean(fmt(2) label(Mean)) "'
local table_cell_str `" `table_cell_str' p50(fmt(2) label(Median)) "'
local table_cell_str `" `table_cell_str' sd(fmt(2) label(Std. Dev.)) "'
local table_cell_str `" `table_cell_str' min(fmt(2) label(Min)) "'
local table_cell_str `" `table_cell_str' max(fmt(2) label(Max)) "'
local table_cell_str `" `table_cell_str' count(fmt(0) label(Obs.)) "'


************************
* auction summary for shock sample
eststo r_summ_sub: estpost tabstat ///
	`auc_stats' `auc_stats_b2c' `auc_stats_frac' , ///
    statistics(mean median sd min max count) columns(statistics) 
esttab r_summ_sub using ../tables/t1a_auc_summ.tex , replace ///
    cells( "`table_cell_str'" ) noobs label nonumbers 


********************************************************************************
********************************************************************************
* number of auctions by term per year (full sample)
n di "number of auctions per year"

* all note/bond auctions
use ../data/td_auction_data_clean.dta, clear
keep if type=="Note" | type=="Bond"
* dollar variables in billions
gen double offeringAmount_bil = offeringAmount / 1e9
gen double totalTendered_bil = totalTendered / 1e9

* stacked bar plot
* labels for term years
label define auc_terms 2 "2-Year" 3 "3-Year" 4 "4-Year" 5 "5-Year" ///
    7 "7-Year" 10 "10-Year" 15 "15-Year" 20 "20-Year" 30 "30-Year" 
label values term_group auc_terms
gen _ind_auc = 1
* use as base color for bars
local clr "0 128 131"
graph bar (count) _ind_auc if auctionDateYear>=1980 & auctionDateYear<=2019, ///
	over(term_group) over(auctionDateYear, relabel(1 "1980" ///
		2 " " 3 " " 4 " " 5 " " 6 "1985" ///
		7 " " 8 " " 9 " " 10 " " 11 "1990" ///
		12 " " 13 " " 14 " " 15 " " 16 "1995" ///
		17 " " 18 " " 19 " " 20 " " 21 "2000" ///
		22 " " 23 " " 24 " " 25 " " 26 "2005" ///
		27 " " 28 " " 29 " " 30 " " 31 "2010" ///
		32 " " 33 " " 34 " " 35 " " 36 "2015" ///
		37 " " 38 " " 39 " " 40 " " ///
		) ) ///
	bar(1, color(`clr'*0.1) ) bar(2, color(`clr'*0.2) ) bar(3, color(`clr'*0.3) ) ///
	bar(4, color(`clr'*0.4) ) bar(5, color(`clr'*0.5) ) bar(6, color(`clr'*0.6) ) ///
	bar(7, color(`clr'*0.7) ) bar(8, color(`clr'*0.8) ) bar(9, color(`clr'*0.9) ) ///
	asyvars stack ytitle("Number of Auctions") ///
	legend(cols(3) colgap(10) position(10) ring(0)) nodraw
drop _ind_auc
* display with correct size and export
local fname f1a_n_aucs_year
graph rename Graph `fname', replace
graph display `fname', xsize(8) ysize(5)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace


n di "dollar value of auctions per year"
* use as base color for bars
local clr "0 128 131"
graph bar offeringAmount_bil if auctionDateYear>=1980 & auctionDateYear<=2019, ///
	over(term_group) over(auctionDateYear, relabel(1 "1980" ///
		2 " " 3 " " 4 " " 5 " " 6 "1985" ///
		7 " " 8 " " 9 " " 10 " " 11 "1990" ///
		12 " " 13 " " 14 " " 15 " " 16 "1995" ///
		17 " " 18 " " 19 " " 20 " " 21 "2000" ///
		22 " " 23 " " 24 " " 25 " " 26 "2005" ///
		27 " " 28 " " 29 " " 30 " " 31 "2010" ///
		32 " " 33 " " 34 " " 35 " " 36 "2015" ///
		37 " " 38 " " 39 " " 40 " " ///
		) ) ///
	bar(1, color(`clr'*0.1) ) bar(2, color(`clr'*0.2) ) bar(3, color(`clr'*0.3) ) ///
	bar(4, color(`clr'*0.4) ) bar(5, color(`clr'*0.5) ) bar(6, color(`clr'*0.6) ) ///
	bar(7, color(`clr'*0.7) ) bar(8, color(`clr'*0.8) ) bar(9, color(`clr'*0.9) ) ///
	asyvars stack ytitle("Dollar Value (Billions) of Auctions") ///
	legend(cols(3) colgap(10) position(10) ring(0)) nodraw
* display with correct size and export
local fname f1a_dollar_aucs_year
graph rename Graph `fname', replace
graph display `fname', xsize(8) ysize(5)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace

* combine with correct size and export
local fname f1a_bar_aucs_year
graph combine f1a_n_aucs_year f1a_dollar_aucs_year , ///
	rows(2) imargin(small) xsize(8) ysize(5) name(`fname', replace)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace



********************************************************************************
********************************************************************************
* time series of bid-to-cover, all and by term (in sample)
use ../data/auction_notesbonds_govpx_shock.dta, clear
n di "b2c time series"

sort auctionDate cusip
* year-only date format for plots
format auctionDate %tdCY

* date range: 1995 to 2017; keep axis tight
local xmin `=mdy(1,1,1995)'
local xmax `=mdy(12,31,2017)'
local xlabels1 `=mdy(1,1,1995)' `=mdy(1,1,2000)' `=mdy(1,1,2005)' ///
	`=mdy(1,1,2010)' `=mdy(1,1,2015)' 
local xlabels2 `=mdy(1,1,1995)' `=mdy(1,1,2005)' `=mdy(1,1,2015)'

* levels
twoway line b2c_all auctionDate, ///
	ytitle("Bid-to-Cover") xtitle("") ///
	xscale(r(`xmin' `xmax')) xlabel(`xlabels1') nodraw
* display with correct size and export
local fname f1a_b2c_timeseries
graph rename Graph `fname', replace
graph display `fname', xsize(8) ysize(5)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace

* changes
twoway line D_b2c_all auctionDate, ///
	ytitle("Bid-to-Cover") xtitle("") ///
	xscale(r(`xmin' `xmax')) xlabel(`xlabels1') nodraw
* display with correct size and export
local fname f1a_D_b2c_timeseries
graph rename Graph `fname', replace
graph display `fname', xsize(8) ysize(5)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace


* insert a "missing" value for when 3 and 30y were not issued
* to make sure plot does not connect lines before and after
set obs `=_N+1'
replace term_group = 3 in `=_N'
replace auctionDate = mdy(1, 1, 2001) in `=_N'
set obs `=_N+1'
replace term_group = 30 in `=_N'
replace auctionDate = mdy(1, 1, 2003) in `=_N'
sort auctionDate cusip


levelsof term_group if auctionDate>=`xmin', local(mat_groups)
foreach m of local mat_groups {
	n di "`m'"
	* only label left/bottom figures
	if `m'==2 | `m'==5 | `m'==10 local y_axis_str ytitle("Bid-to-Cover")
	else local y_axis_str ytitle("")
	
	twoway line b2c_all auctionDate ///
		if auctionDate>=`xmin' & term_group==`m', title("`m'-Year") ///
		cmissing(n) xtitle("") xscale(r(`xmin' `xmax')) xlabel(`xlabels2') ///
		`y_axis_str' ///
		name("b2c_mat`m'", replace) nodraw
}
* combine with correct size and export
local fname f1a_b2c_timeseries_by_term
graph combine b2c_mat2 b2c_mat3 b2c_mat5 b2c_mat7 b2c_mat10 b2c_mat30 , ///
	rows(3) imargin(small) ycommon xsize(5) ysize(7) name(`fname', replace)
graph export ../figs/`fname'.pdf, replace
graph export ../figs/`fname'.eps, replace



